package edison.jcr.security;

import java.util.HashMap;
import java.util.Map;

import javax.jcr.security.Privilege;

public class PrivilegeImpl implements Privilege {

	public static final Privilege JCR_ADD_CHILD_NODES = new PrivilegeImpl(Privilege.JCR_ADD_CHILD_NODES, true, false);
	public static final Privilege JCR_ALL = new JCR_ALL();
	public static final Privilege JCR_LIFECYCLE_MANAGEMENT = new PrivilegeImpl(Privilege.JCR_LIFECYCLE_MANAGEMENT);
	public static final Privilege JCR_LOCK_MANAGEMENT = new PrivilegeImpl(Privilege.JCR_LOCK_MANAGEMENT);
	public static final Privilege JCR_MODIFY_ACCESS_CONTROL = new PrivilegeImpl(Privilege.JCR_MODIFY_ACCESS_CONTROL);
	public static final Privilege JCR_MODIFY_PROPERTIES = new PrivilegeImpl(Privilege.JCR_MODIFY_PROPERTIES, true, false);
	public static final Privilege JCR_NODE_TYPE_MANAGEMENT = new PrivilegeImpl(Privilege.JCR_NODE_TYPE_MANAGEMENT);
	public static final Privilege JCR_READ = new PrivilegeImpl(Privilege.JCR_READ);
	public static final Privilege JCR_READ_ACCESS_CONTROL = new PrivilegeImpl(Privilege.JCR_READ_ACCESS_CONTROL);
	public static final Privilege JCR_REMOVE_CHILD_NODES = new PrivilegeImpl(Privilege.JCR_REMOVE_CHILD_NODES, true, false);
	public static final Privilege JCR_REMOVE_NODE = new PrivilegeImpl(Privilege.JCR_REMOVE_NODE, true, false);
	public static final Privilege JCR_RETENTION_MANAGEMENT = new PrivilegeImpl(Privilege.JCR_RETENTION_MANAGEMENT);
	public static final Privilege JCR_VERSION_MANAGEMENT = new PrivilegeImpl(Privilege.JCR_VERSION_MANAGEMENT);
	public static final Privilege JCR_WRITE = new JCR_WRITE();
	public static final Map<String, Privilege> MAP = new HashMap<String, Privilege>();

	static {
		MAP.put(Privilege.JCR_ADD_CHILD_NODES, JCR_ADD_CHILD_NODES);
		MAP.put(Privilege.JCR_ALL, JCR_ALL);
		MAP.put(Privilege.JCR_LIFECYCLE_MANAGEMENT, JCR_LIFECYCLE_MANAGEMENT);
		MAP.put(Privilege.JCR_LOCK_MANAGEMENT, JCR_LOCK_MANAGEMENT);
		MAP.put(Privilege.JCR_MODIFY_ACCESS_CONTROL, JCR_MODIFY_ACCESS_CONTROL);
		MAP.put(Privilege.JCR_MODIFY_PROPERTIES, JCR_MODIFY_PROPERTIES);
		MAP.put(Privilege.JCR_NODE_TYPE_MANAGEMENT, JCR_NODE_TYPE_MANAGEMENT);
		MAP.put(Privilege.JCR_READ, JCR_READ);
		MAP.put(Privilege.JCR_READ_ACCESS_CONTROL, JCR_READ_ACCESS_CONTROL);
		MAP.put(Privilege.JCR_REMOVE_CHILD_NODES, JCR_REMOVE_CHILD_NODES);
		MAP.put(Privilege.JCR_REMOVE_NODE, JCR_REMOVE_NODE);
		MAP.put(Privilege.JCR_RETENTION_MANAGEMENT, JCR_RETENTION_MANAGEMENT);
		MAP.put(Privilege.JCR_VERSION_MANAGEMENT, JCR_VERSION_MANAGEMENT);
		MAP.put(Privilege.JCR_WRITE, JCR_WRITE);
	}

	
	
	private final String name;
	private final boolean isAbstract;
	private final boolean isAggregate;

	private PrivilegeImpl(String name) {
		this(name, false, false);
	}

	private PrivilegeImpl(String name, boolean isAbstract, boolean isAggregate) {
		this.name = name;
		this.isAbstract = isAbstract;
		this.isAggregate = isAggregate;
	}

	public String getName() {
		return name;
	}

	public boolean isAbstract() {
		return isAbstract;
	}

	public boolean isAggregate() {
		return isAggregate;
	}

	public Privilege[] getDeclaredAggregatePrivileges() {
		return new Privilege[] {};
	}

	public Privilege[] getAggregatePrivileges() {
		return new Privilege[] {};
	}

	@Override
	public String toString() {
		return name;
	}

	static class JCR_ALL extends PrivilegeImpl {
		public JCR_ALL() {
			super(Privilege.JCR_ALL, false, true);
		}

		@Override
		public Privilege[] getDeclaredAggregatePrivileges() {
			return new Privilege[] {

			PrivilegeImpl.JCR_LIFECYCLE_MANAGEMENT,

			PrivilegeImpl.JCR_LOCK_MANAGEMENT,

			PrivilegeImpl.JCR_MODIFY_ACCESS_CONTROL,

			PrivilegeImpl.JCR_NODE_TYPE_MANAGEMENT,

			PrivilegeImpl.JCR_READ,

			PrivilegeImpl.JCR_READ_ACCESS_CONTROL,

			PrivilegeImpl.JCR_RETENTION_MANAGEMENT,

			PrivilegeImpl.JCR_VERSION_MANAGEMENT,

			PrivilegeImpl.JCR_WRITE

			};
		}

		@Override
		public Privilege[] getAggregatePrivileges() {

			return new Privilege[] {

			PrivilegeImpl.JCR_LIFECYCLE_MANAGEMENT,

			PrivilegeImpl.JCR_LOCK_MANAGEMENT,

			PrivilegeImpl.JCR_MODIFY_ACCESS_CONTROL,

			PrivilegeImpl.JCR_NODE_TYPE_MANAGEMENT,

			PrivilegeImpl.JCR_READ,

			PrivilegeImpl.JCR_READ_ACCESS_CONTROL,

			PrivilegeImpl.JCR_RETENTION_MANAGEMENT,

			PrivilegeImpl.JCR_VERSION_MANAGEMENT,

			PrivilegeImpl.JCR_WRITE,

			PrivilegeImpl.JCR_MODIFY_PROPERTIES,

			PrivilegeImpl.JCR_ADD_CHILD_NODES,

			PrivilegeImpl.JCR_REMOVE_NODE,

			PrivilegeImpl.JCR_REMOVE_CHILD_NODES

			};
		}
	}

	static class JCR_WRITE extends PrivilegeImpl {

		public JCR_WRITE() {
			super(Privilege.JCR_WRITE, false, true);
		}

		@Override
		public Privilege[] getDeclaredAggregatePrivileges() {
			return new Privilege[] {

			PrivilegeImpl.JCR_MODIFY_PROPERTIES,

			PrivilegeImpl.JCR_ADD_CHILD_NODES,

			PrivilegeImpl.JCR_REMOVE_NODE,

			PrivilegeImpl.JCR_REMOVE_CHILD_NODES

			};
		}

		@Override
		public Privilege[] getAggregatePrivileges() {
			return getDeclaredAggregatePrivileges();
		}

	}

}